// checksum 0xfd0b version 0x20001
/*
 * Copyright (c) 2011 Nokia Corporation.
 */

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCore/QCoreApplication>
#include <QGraphicsRectItem>
#include <QLinearGradient>
#include <QDebug>

#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK

void View::setOrientation(ScreenOrientation orientation)
{
#if defined(Q_OS_SYMBIAN)
    // If the version of Qt on the device is < 4.7.2, that attribute won't work
    if (orientation != ScreenOrientationAuto) {
        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
            return;
        }
    }
#endif // Q_OS_SYMBIAN

    Qt::WidgetAttribute attribute;
    switch (orientation) {
#if QT_VERSION < 0x040702
    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
    case ScreenOrientationLockPortrait:
        attribute = static_cast<Qt::WidgetAttribute>(128);
        break;
    case ScreenOrientationLockLandscape:
        attribute = static_cast<Qt::WidgetAttribute>(129);
        break;
    default:
    case ScreenOrientationAuto:
        attribute = static_cast<Qt::WidgetAttribute>(130);
        break;
#else // QT_VERSION < 0x040702
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_LockPortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_LockLandscapeOrientation;
        break;
    default:
    case ScreenOrientationAuto:
        attribute = Qt::WA_AutoOrientation;
        break;
#endif // QT_VERSION < 0x040702
    };
    setAttribute(attribute, true);
}

void View::showExpanded()
{
#ifdef Q_OS_SYMBIAN
    showFullScreen();
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    showMaximized();
#else
    show();
#endif
}

View::View( QWidget * parent) : QDeclarativeView ( parent )
{
    init();
}

View::View( const QUrl & source, QWidget * parent ) : QDeclarativeView ( source, parent )
{
    init();
}

void View::init()
{
    /***********************************************************************************************
    *Initialize the view, set the frame count and fps to 0.                                        *
    *Set up the view to cache its background.                                                      *
    ***********************************************************************************************/
    m_frames = 0;
    m_fps = 0.0;
    setCacheMode(QGraphicsView::CacheBackground);
    setBackgroundBrush(Qt::black);

    /***********************************************************************************************
    *Prevent the viewport form accepting any focus.                                                *
    *Set up the view to accept click and tab focus.                                                *
    *Set up the scene to have sticky focus.                                                        *
    ***********************************************************************************************/
    viewport()->setFocusPolicy(Qt::NoFocus);
    setFocusPolicy(Qt::StrongFocus);
    scene()->setStickyFocus(true);
}

void View::resizeEvent(QResizeEvent *event)
{
    /***********************************************************************************************
    *Call the base classes resizeEvent and scale the views contents so that it fits in the view.   *
    ***********************************************************************************************/
    QDeclarativeView::resizeEvent(event);
    fittInView( QRect(0,0,1600,1000));
    viewport()->update();
}

void View::fittInView(const QRectF &rect)
{
    QDeclarativeView::fitInView(rect, Qt::KeepAspectRatio);
}

void View::scrollContentsBy ( int, int)
{
    /***********************************************************************************************
    *Prevent scrolling.                                                                            *
    ***********************************************************************************************/
}

void View::paintEvent ( QPaintEvent * event )
{
    QDeclarativeView::paintEvent(event);
    /***********************************************************************************************
    *Calculate fps.                                                                                *
    ***********************************************************************************************/
    if (!(m_frames % 25)) {
        m_fps = m_frames /(m_time.elapsed() / 1000.0);
        /*******************************************************************************************
        *Notify that there is a new fps reading.                                                   *
        *******************************************************************************************/
        emit fpsChanged();
        m_time.start();
        m_frames = 0;
    }
    ++m_frames;
}
